home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Lib / src / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-24  |  1.9 KB  |  105 lines

  1. /******************************************************************************
  2.  
  3.     MODULE
  4.     malloc.c
  5.  
  6.     DESCRIPTION
  7.     replacements for malloc/strdup/free
  8.     do achieve some mungwall like behaviour
  9.  
  10.     HISTORY
  11.     23-11-94 b_boll created
  12.     $Log: malloc.c $
  13.  
  14. ******************************************************************************/
  15.  
  16. /**************************************
  17.           Includes
  18. **************************************/
  19.  
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. /**************************************
  24.       Internal Defines & Structures
  25. **************************************/
  26.  
  27. struct Header {
  28.     int size;
  29.     char *file;
  30.     int line;
  31. }; /* struct Header */
  32.       /*
  33. findr ˜ ( )
  34.         */
  35. #define HEADER sizeof (struct Header)
  36. #define FOOTER 8
  37.  
  38. #define NEW    0xD0
  39. #define DEAD    0xDE
  40. #define END    0xED
  41.  
  42. /**************************************
  43.          Implementation
  44. **************************************/
  45.  
  46.  
  47.  
  48. void *xmalloc (int size, char *file, int line)
  49. {
  50.     struct Header *rv;
  51.  
  52.     if ((rv = malloc(size + HEADER + FOOTER))) {
  53.     rv->size = size;
  54.     rv->file = file;
  55.     rv->line = line;
  56.     rv ++;
  57.     {
  58.         unsigned char *x;
  59.         int i;
  60.         x = (void *)rv;
  61.  
  62.         for (i = size;   i; --i, ++x)
  63.         *x = NEW;
  64.  
  65.         for (i = FOOTER; i; --i, ++x)
  66.         *x = END;
  67.     }
  68.     } /* if */
  69.     return rv;
  70. } /* xmalloc */
  71.  
  72. char *xstrdup (char *str, char *file, int line)
  73. {
  74.     char *pstr;
  75.     if ((pstr = xmalloc (strlen (str) + 1, file, line))) {
  76.     strcpy (pstr, str);
  77.     } /* if */
  78.     return pstr;
  79. } /* xstrdup */
  80.  
  81. void xfree (void *ptr, char *file, int line)
  82. {
  83.     if (ptr) {
  84.     struct Header *rv;
  85.     rv = ptr;
  86.     --rv;
  87.     {
  88.         unsigned char *x;
  89.         int   i;
  90.  
  91.         x = ptr;
  92.         for (i = rv->size; i; --i, ++x)
  93.         *x = DEAD;
  94.     }
  95.     free  (rv);
  96.     } /* if */
  97. } /* xfree */
  98.  
  99.  
  100.  
  101.  
  102. /******************************************************************************
  103. *****  END malloc.c
  104. ******************************************************************************/
  105.